#include <graphics.h>
#include <conio.h>
#include<stdlib.h>
#include<time.h>
#include<stdio.h>
#define width 640
#define height 480
#define num 32  
double ball_x,ball_y;
double ball_vx,ball_vy;
int radius;
int bar_x,bar_y;
int bar_left,bar_top,bar_right,bar_bottom;
int isBrick[num];
int z_left,z_right,z_top,z_bottom;
int z_height,z_width;
int w,h;
int overNum;
char s[2];
char c;
int i,j;
IMAGE gameOver;
void initdata();
void isOrNot();
void clear();
void move();
void dangMove();
void show();
void Over();
void main(){
	initgraph(width,height);
	initdata();
	isOrNot();
	BeginBatchDraw();
	while(1){
        clear();
		move();
		dangMove();
		show();
		if(ball_y>height-radius){
		Over();
		break;
		}
		Sleep(3);
		FlushBatchDraw();
	}
	EndBatchDraw();
	_getch();
	closegraph();
}

void initdata(){
	ball_x=width/2;
	ball_y=height-50;
	ball_vx=1;
	ball_vy=1;
	radius=13;
	bar_top=450;
	bar_bottom=480;
	bar_x=width/2;
	z_height=30;
	z_width=80;
	overNum=0;
}
void isOrNot(){
	for(int i=0;i<num;i++){
		isBrick[i]=1;
	}
}
void clear(){
    setcolor(BLACK);
	setfillcolor(BLACK);
	
    fillcircle(ball_x,ball_y,radius);
	
    fillrectangle(bar_left,bar_top,bar_right,bar_bottom);
	
	for(int i=0;i<num;i++){
		if(!isBrick[i]){
				w=i%8;
				z_left=z_width*w;
				z_right=z_left+z_width;
				h=i/8;
				z_top=z_height*h;
				z_bottom=z_height*(h+1);
				fillrectangle(z_left,z_top,z_right,z_bottom);
		}
	}
}
void move(){
	
    ball_x=ball_x+ball_vx;
	ball_y=ball_y+ball_vy;
	if(ball_x<=radius||ball_x>=width-radius){
		ball_vx=-ball_vx;
	}
	if(ball_y<=radius){
		ball_vy=-ball_vy;
	}
	
	if(ball_x>=bar_left&&ball_x<=bar_right&&ball_y+30+radius>=height){
		ball_vy=-ball_vy;
	}
	if(ball_x<bar_left&&ball_x+radius>=bar_left&&ball_y+30+radius>=height){
		ball_vx=-ball_vx;
	}
	if(ball_x>bar_right&&ball_x<bar_right+radius&&ball_y+30+radius>=height){
		ball_vx=-ball_vx;
	}
	if(((ball_x-bar_left)*(ball_x-bar_left)+(ball_y-bar_top)*(ball_y-bar_top))<=(radius*radius)&&(ball_vx>0)&&(ball_vy>0)||
		((ball_x-bar_right)*(ball_x-bar_right)+(ball_y-bar_top)*(ball_y-bar_top))<=(radius*radius)&&(ball_vx<0)&&(ball_vy>0)){
			ball_vx=-ball_vx;ball_vy=-ball_vy;
	}
	
	for(i=0;i<num;i++){
		if(isBrick[i]){
            w=i%8;
			z_left=z_width*w;
			z_right=z_left+z_width;
			h=i/8;
			z_top=z_height*h;
			z_bottom=z_height*(h+1);
			
			if((ball_y==z_bottom+radius)&&(ball_x>=z_left)&& (ball_x< z_right)){
				isBrick[i]=0;
				ball_vy=-ball_vy;
			}
			
			if((ball_y==z_top-radius)&&(ball_x>=z_left)&& (ball_x< z_right)){
				isBrick[i]=0;
				ball_vy=-ball_vy;
			}
			
			if((ball_x==z_right+radius)&&(ball_y<z_bottom)&&(ball_y>=z_top)){
				isBrick[i]=0;
				ball_vx=-ball_vx;
			}
		
			if((ball_x==z_left-radius)&&(ball_y<z_bottom)&&(ball_y>=z_top)){
				isBrick[i]=0;
				ball_vx=-ball_vx;
			}
		
			if((((ball_x-z_right)*(ball_x-z_right)+(ball_y-z_bottom)*(ball_y-z_bottom))<=(radius*radius))&&(ball_vx<0)&&(ball_vy<0)||
				((ball_x-z_right)*(ball_x-z_right)+(ball_y-z_top)*(ball_y-z_top))<=(radius*radius)&&(ball_vx<0)&&(ball_vy>0)||
				((ball_x-z_left)*(ball_x-z_left)+(ball_y-z_bottom)*(ball_y-z_bottom))<=(radius*radius)&&(ball_vx>0)&&(ball_vy<0)||
				((ball_x-z_right)*(ball_x-z_right)+(ball_y-z_top)*(ball_y-z_top))<=(radius*radius)&&(ball_vx>0)&&(ball_vy>0)){
				isBrick[i]=0;
				ball_vx=-ball_vx;ball_vy=-ball_vy;
			}
		}
	}
}
void dangMove(){
  
	bar_left=bar_x-70;
	bar_right=bar_x+70;
	if(_kbhit()){
	     c=_getch();
	     switch(c){
	         case 'a':
			     if(bar_left<=0)
			         bar_left=0;
			     else  
				     bar_x-=10;
		             break;
	         case 'd':
		         if(bar_right>=640)
					bar_right=640;
				 else  
				    bar_x+=10;
	 	            break;
			 }
		}
}
void show(){
    setfillcolor(CYAN);
	fillcircle(ball_x,ball_y,radius);
	setcolor(WHITE);
	setfillcolor(RED);
	fillrectangle(bar_left,bar_top,bar_right,bar_bottom);
	
	for(i=0;i<num;i++){
		if(isBrick[i]){
			w=i%8;
			z_left=z_width*w;
			z_right=z_left+z_width;
			h=i/8;
			z_top=z_height*h;
			z_bottom=z_height*(h+1);
			setcolor(WHITE);
			setfillcolor(RGB(50*w*h+20*w,50+40*h,50*(8-h)));			
			fillrectangle(z_left,z_top,z_right,z_bottom);
		}
	}
}
void Over(){
    
	loadimage(&gameOver,_T("D:/code/ƴͼ/lose1.jpg"));
	putimage(0,0,&gameOver);
}